package com.hsnn.medstgmini.pro.base.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.controller.DicPharController;
import com.hsnn.medstgmini.base.dict.model.DicDrugname;
import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.base.dict.service.DicDrugnameManager;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.pro.base.model.BaseGoods;
import com.hsnn.medstgmini.pro.base.model.form.BaseShortmedicineForm;
import com.hsnn.medstgmini.pro.base.model.form.ShortMedicine;
import com.hsnn.medstgmini.pro.base.service.DrugShortageManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DicSysUtil;
import com.hsnn.medstgmini.util.Pagination;


/**
 * @category 缺货申报
 * @author 罗超
 * @date 2016年3月7日09:21:01
 */
@Controller
@RequestMapping("/DrugShortage")
public class DrugShortageController {

	
	private static final Logger log = Logger.getLogger(DicPharController.class);
	
	@Autowired
	private DrugShortageManager drugShortageManager;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpSession session;
	
	@Autowired
	private DicDrugnameManager dicDrugnameManager;
	
	@Autowired
	private DrugShortageManager drugShortage;
	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}


	/**
	 * 跳转到短缺药品申报页面
	 * @return 
	 */
	@RequestMapping("/toDrugShortage")
	public String DrugShorttage() {
		List<DicSys> mlist = DicSysUtil.getDicListByClassCode(Constants.PRO_METHOD);
		request.setAttribute("list", mlist);
		return "/pro/base/shortage/drugShortage";
	}
	/**
	 * 获取药品列表
	 * @param from
	 * @return
	 */
	@RequestMapping("/getDrugShortage")
	@ResponseBody
	public Pagination getDrugShortage(BaseGoods form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		HospInfo hospInfo = user.getInfo();
		form.setHospCode(hospInfo.getHospCode());
		page.setForm(form);
		return drugShortageManager.getDataList(page, hospInfo);
	}

	

	
	/**
	 * 跳转到查询页面
	 * @category  
	 * @return
	 * @date 2016年3月7日14:12:13
	 */
	@RequestMapping("/toList")
	public String toDeclare() {		
		return "/pro/base/shortage/drugShortageSee";
	}
	
	@RequestMapping("/toUpdate")
	public String toUpdate(Model model){
		String shortmedicine_id = request.getParameter("shortmedicine_id");
		Map<String, Object> info = drugShortageManager.getDrugInfo(shortmedicine_id);
		model.addAttribute("info", info);
		return "/pro/base/shortage/updateDrugShortage";
	}
	
	@RequestMapping("/toCancel")
	public String toCancel(Model model){
		String shortmedicine_id = request.getParameter("shortmedicine_id");
		Map<String, Object> info = drugShortageManager.getDrugInfo(shortmedicine_id);
		model.addAttribute("info", info);
		return "/pro/base/shortage/cancelDrugShortage";
	}
	/**
	 * 显示已申报短缺药品
	 * @category  
	 * @return
	 * @date 2016年3月7日14:12:13
	 */
	@ResponseBody
	@RequestMapping("/getShortageInfo")
	public Pagination getShortageInfo(ShortMedicine form){
		Pagination page = new Pagination(request);
		
		String drugName = request.getParameter("drugName"); //'通用名',
		String formName = request.getParameter("formName");//'剂型',
		String specName = request.getParameter("specName"); //'规格',
		String unitName = request.getParameter("unitName"); //'包装单位',
		String compName = request.getParameter("compName"); // '生产企业',
		String drugAttr = request.getParameter("drugAttr");  // '药品属性',
		String drugMedicine = request.getParameter("drugMedicine"); // '国药准字号',
		
		if(StringUtils.isNoneBlank(drugName)){
			page.getConditions().put("drugName", drugName);
		}
		if(StringUtils.isNoneBlank(formName)){
			page.getConditions().put("formName", formName);
		}
		if(StringUtils.isNoneBlank(specName)){
			page.getConditions().put("specName", specName);
		}
		if(StringUtils.isNoneBlank(unitName)){
			page.getConditions().put("unitName", unitName);
		}
		if(StringUtils.isNoneBlank(compName)){
			page.getConditions().put("compName", compName);
		}
		if(StringUtils.isNoneBlank(drugAttr)){
			page.getConditions().put("drugAttr", drugAttr);
		}
		if(StringUtils.isNoneBlank(drugName)){
			page.getConditions().put("drugMedicine", drugMedicine);
		}
		try {
			HospInfo hospInfo = getUser().getInfo();
			form.setHosp_code(hospInfo.getHospCode());
			page.setForm(form);
			page = drugShortage.getShortageInfo(page,hospInfo);
			page.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	@ResponseBody
	@RequestMapping("/updateShortageBySMI")
	public Pagination updateShortageBySMI(ShortMedicine sm) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		sm.setUpd_user(user.getId());
		try {
			drugShortage.UpdateBySMI(sm);
			page.setSuccess(true);
	}  catch (Exception e) {
		log.error("",e);
		page.setSuccess(false);
	}
	return page;
	}
	
	@ResponseBody
	@RequestMapping("/updateShortageBySS")
	public Pagination updateShortageBySS(ShortMedicine sm) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		sm.setUpd_user(user.getId());
		drugShortage.UpdateBySS(sm);
		page.setSuccess(true);
		return page;
	}

	/**
	 * 跳转到上报页面
	 * @return
	 */
	@RequestMapping ("/toReported")
	public String toDrugShortageEdit(Model model, String goodsCode, String projId){
		Map<String, Object> detail = drugShortageManager.getDrugShortageDetail(goodsCode,projId);
		model.addAttribute("detail", detail);
		return "/pro/base/shortage/drugShortageReported";
	}
	/**
	 * 上报中标药品
	 * @param form
	 * @return
	 */
	@RequestMapping("/saveDrugInfoRep")
	@ResponseBody
	public Pagination saveProdDrugInfoRep(BaseShortmedicineForm form) {
		Pagination page = new Pagination(request);
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		form.setUpdUser(user.getId());
		try {
				drugShortageManager.saveDrugInfoRep(form, getUser());
				page.setSuccess(true);
		}  catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	/**
	 * @category 判断医疗机构名称重复性
	 * @param model
	 * @param hospInfoLog
	 * @return
	 */
	@RequestMapping("testRepByDrugNameHosp")
	@ResponseBody
	public Boolean testRepByDrugNameHosp(){
		String drugName = request.getParameter("drugName");
		String formName = request.getParameter("formName");//'剂型',
		String specName = request.getParameter("specName"); //'规格',
		String factor = request.getParameter("factor"); //'规格',
		String unitName = request.getParameter("unitName"); //'包装单位',
		String compName = request.getParameter("compName"); // '生产企业',
		String drugAttr = request.getParameter("drugAttr");  // '药品属性',
		if(null!=drugName && !"".equals(drugName)){
			List<BaseShortmedicineForm> list = drugShortageManager.getHospInfoListByDrugNameHosp(drugName,formName,specName,factor,unitName,compName,drugAttr);
			if(list.size() > 0){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping ("/add")
	public String toDrugShortageAdd(){
		return "/pro/base/shortage/drugShortageAdd";
	}
	
	
	/**
	 * @category 判断医疗机构名称重复性
	 * @param model
	 * @param hospInfoLog
	 * @return
	 */
	@RequestMapping("testRepByDrugName")
	@ResponseBody
	public String testRepByDrugName(){
		String drugName = request.getParameter("drugName");
		String formName = request.getParameter("formName");//'剂型',
		String specName = request.getParameter("specName"); //'规格',
		String factor = request.getParameter("factor"); //'转换系数',
		String unitName = request.getParameter("unitName"); //'包装单位',
		String compName = request.getParameter("compName"); // '生产企业',
		String drugAttr = request.getParameter("drugAttr");  // '药品属性',
		if(null!=drugName && !"".equals(drugName)){
			List<BaseShortmedicineForm> lists = drugShortageManager.getHospInfoListByDrugNameHosp(drugName,formName,specName,factor,unitName,compName,drugAttr);
			if(lists.size() > 0){
				return "药品当月已经上报，请下月在上报！";
			}
			List<BaseGoods> list = drugShortageManager.getHospInfoListByDrugName(drugName,formName,specName,factor,unitName,compName,drugAttr);
			if(list.size() > 0){
				return "通用名已经在中标药品列表中，请返回列表选择！";
			}
		}
		return "";
	}
	
	/**
	 * 保存新增
	 * @param form
	 * @return
	 */
	@RequestMapping("/saveDrugInfo")
	@ResponseBody
	public Pagination saveProdDrugInfo(BaseShortmedicineForm form, DicDrugname dicDrugname) {
		Pagination page = new Pagination(request);
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		form.setUpdUser(user.getId());
		try {
				drugShortageManager.saveDrugInfo(form, getUser());
				page.setSuccess(true);
		}  catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
			

	
	
	
	/**
	 * 卫生局跳转到短缺药品查看页面
	 * @return
	 */
	@RequestMapping ("/toDrugShortageWSJ")
	public String toDrugShortageWSJ(){
		return "/pro/base/shortage/drugShortageWSJ";
	}
	
	/**
	 * 卫生局查看下属医院的药品短缺列表
	 * @param form
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getShortageInfoWSJ")
	public Pagination getShortageInfoWSJ(BaseShortmedicineForm form){
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String orgId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			orgId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			orgId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("orgId", orgId);
		page.setForm(form);
		return drugShortageManager.getShortageInfoWSJ(page);
	}

	/**
	 * 中心端短缺药品查看
	 * @category  
	 * @return
	 * @date 2016年3月16日10:58:13
	 */
	@RequestMapping("/toFind")
	public String toCenterView() {		
		return "/pro/base/shortage/centerDrugShortage";
	}
	/**
	 * 获取短缺药品列表
	 * @param from
	 * @return
	 */
	@RequestMapping("/getAllShortage")
	@ResponseBody
	public Pagination getAllShortage() {
		Pagination page = new Pagination(request);
		String drugName = request.getParameter("drugName"); //'通用名',
		String compName = request.getParameter("compName"); //'生产企业',
		if(StringUtils.isNoneBlank(drugName)){
			page.getConditions().put("drugName", drugName);
		}
		if(StringUtils.isNoneBlank(compName)){
			page.getConditions().put("compName", compName);
		}
		try {
			page = drugShortage.getAllShortage(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}


	
}
